﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using FlyHorse.LIMS.Model;
using FlyHorse.LIMS.WinApp.Utils;

namespace FlyHorse.LIMS.WinApp.DataManageForms.ReportProductForms
{
    public partial class FillSemiDataForm : DevExpress.XtraEditors.XtraForm
    {
        BLL.ToolBll toolBll = new BLL.ToolBll();
        OJCB4Entities db = new OJCB4Entities();
        Action<ReportSemi> _FillData;
        public FillSemiDataForm(Action<ReportSemi> FillData)
        {
            InitializeComponent();
            _FillData = FillData;
        }

        private void FillSemiDataForm_Load(object sender, EventArgs e)
        {
            this.gridView1.FocusRectStyle = DevExpress.XtraGrid.Views.Grid.DrawFocusRectStyle.RowFocus;
            this.gridView1.OptionsBehavior.Editable = false;
            this.gridView1.OptionsSelection.EnableAppearanceFocusedCell = false;

            

            //绑定grid里的样品名称
            List<Good> goodsAllList = toolBll.GetAllGoodList();
            repositoryItemLookUpEdit1.DataSource = goodsAllList;
            ////绑定grid里的取样位置
            repositoryItemLookUpEdit2.DataSource = toolBll.GetAllSamplingPlaceList();

            ////绑定grid里的生产线
            repositoryItemLookUpEdit3.DataSource = toolBll.GetProductionLineList();
            ////绑定grid里的班组
            repositoryItemLookUpEdit4.DataSource = toolBll.GetProductionGropList();
            ////绑定grid里的录入人
            repositoryItemLookUpEdit5.DataSource = toolBll.GetAllLoginUersList();
        }

        private void barEditItem1_EditValueChanged(object sender, EventArgs e)
        {
            splashScreenManager1.ShowWaitForm();
            DateTime date = Convert.ToDateTime(this.barEditItem_Date.EditValue);
            DateTime yesterday = date.AddDays(-1);
            DateTime tomorrow = date.AddDays(1);
            var dataList = db.ReportSemis.AsNoTracking().Where(m => m.SendDate >= date && m.SendDate < tomorrow).OrderBy(m=>m.SendDate);
            PagedList<ReportSemi> pagedList = new PagedList<ReportSemi>(dataList, 1, 99999999);
            this.gridControl1.DataSource = TransformReport.TransformSemiReport(pagedList);

            splashScreenManager1.CloseWaitForm();
        }

        private void barLargeButtonItem_Fill_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (this.gridView1.GetFocusedRowCellValue("ReportSemiID") != null)
            {
                string id = Convert.ToString(this.gridView1.GetFocusedRowCellValue("ReportSemiID"));
                ReportSemi report = db.ReportSemis.Where(m => m.ReportSemiID == id).FirstOrDefault();
                if (report != null)
                {
                    _FillData(report);
                    this.Close();
                }

            }
        }
    }
}